﻿namespace Hidistro.SqlDal.Members
{
    using Hidistro.Entities;
    using Hidistro.Entities.Members;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System;
    using System.Data;
    using System.Data.Common;

    public class InpourRequestDao
    {
        private Database database = DatabaseFactory.CreateDatabase();

        public bool AddInpourBlance(InpourRequestInfo inpourRequest)
        {
            if (null == inpourRequest)
            {
                return false;
            }
            DbCommand storedProcCommand = this.database.GetStoredProcCommand("ac_Member_InpourRequest_Create");
            this.database.AddInParameter(storedProcCommand, "InpourId", DbType.String, inpourRequest.InpourId);
            this.database.AddInParameter(storedProcCommand, "TradeDate", DbType.DateTime, inpourRequest.TradeDate);
            this.database.AddInParameter(storedProcCommand, "InpourBlance", DbType.Currency, inpourRequest.InpourBlance);
            this.database.AddInParameter(storedProcCommand, "UserId", DbType.Int32, inpourRequest.UserId);
            this.database.AddInParameter(storedProcCommand, "PaymentId", DbType.String, inpourRequest.PaymentId);
            this.database.AddOutParameter(storedProcCommand, "Status", DbType.Int32, 4);
            this.database.ExecuteNonQuery(storedProcCommand);
            return (((int) this.database.GetParameterValue(storedProcCommand, "Status")) == 0);
        }

        public InpourRequestInfo GetInpourBlance(string inpourId)
        {
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand("SELECT * FROM Hishop_InpourRequest WHERE InpourId = @InpourId;");
            this.database.AddInParameter(sqlStringCommand, "InpourId", DbType.String, inpourId);
            InpourRequestInfo info = null;
            using (IDataReader reader = this.database.ExecuteReader(sqlStringCommand))
            {
                if (reader.Read())
                {
                    info = DataMapper.PopulateInpourRequest(reader);
                }
            }
            return info;
        }

        public bool IsRecharge(string inpourId)
        {
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand("SELECT COUNT(*) FROM Hishop_BalanceDetails WHERE InpourId = @InpourId");
            this.database.AddInParameter(sqlStringCommand, "InpourId", DbType.String, inpourId);
            return (((int) this.database.ExecuteScalar(sqlStringCommand)) > 0);
        }

        public void RemoveInpourRequest(string inpourId)
        {
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand("DELETE FROM Hishop_InpourRequest WHERE InpourId = @InpourId");
            this.database.AddInParameter(sqlStringCommand, "InpourId", DbType.String, inpourId);
            this.database.ExecuteNonQuery(sqlStringCommand);
        }
    }
}

